package com.miet.mp45.filippov.cryptoapplet;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;

import javax.swing.BorderFactory;
import javax.swing.JPanel;


import com.miet.mp45.filippov.cryptoapplet.actionpanels.ActionPanel;
import com.miet.mp45.filippov.cryptoapplet.actionpanels.GenerateCSRPanel;
import com.miet.mp45.filippov.cryptoapplet.actionpanels.GenerateCertificatePanel;
import com.miet.mp45.filippov.cryptoapplet.actionpanels.KeyGenPanel;
import com.miet.mp45.filippov.cryptoapplet.actionpanels.ExpractPanel;
import com.miet.mp45.filippov.cryptoapplet.actionpanels.SignFilePanel;
import com.miet.mp45.filippov.cryptoapplet.actionpanels.events.ProcessingFinishedEvent;
import com.miet.mp45.filippov.cryptoapplet.actionpanels.events.ProcessingFinishedEventListener;
import com.miet.mp45.filippov.cryptoapplet.actionpanels.events.ProcessingStartedEvent;
import com.miet.mp45.filippov.cryptoapplet.actionpanels.events.ProcessingStartedEventListener;
import com.miet.mp45.filippov.log.TextAreaAppender;

public class MainPanel extends javax.swing.JPanel
                       implements ProcessingStartedEventListener,
                                  ProcessingFinishedEventListener
                                  
{
    
    private static final long serialVersionUID = -3078090753676848653L;
    
    private final HashMap<String, ActionPanel> actionsMap = new HashMap<String, ActionPanel>();
    private final String delimiter = "------------------------------------------------------" + System.getProperty("line.separator");
    private javax.swing.JComboBox actionComboBox;
    private javax.swing.JPanel actionSelectionPanel;
    private javax.swing.JPanel actionPanelContainer;
    private javax.swing.Box.Filler filler2;
    private javax.swing.Box.Filler filler3;
    private javax.swing.JScrollPane logScrollPane;
    private javax.swing.JTextArea logTextArea;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JPanel statusPanel;
    
    
    public MainPanel()
    {
        String genKP = "Generate Keypair";
        String sepPK = "Separate Public Key";
        String sigFile = "Sign File";
        String genCSR = "Generate Certificate Request";
        String genCert = "Generate X.509 Certificate";
        
        actionsMap.put(genKP, new KeyGenPanel(this, this));
        actionsMap.put(sepPK, new ExpractPanel(this, this));
        actionsMap.put(sigFile, new SignFilePanel(this, this));
        actionsMap.put(genCSR, new GenerateCSRPanel(this, this));
        actionsMap.put(genCert, new GenerateCertificatePanel(this, this));
        
        initComponents();
        
        TextAreaAppender.setTextArea(logTextArea);
    }

    private void initComponents()
    {

        statusPanel = new javax.swing.JPanel();
        statusLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        actionSelectionPanel = new javax.swing.JPanel();
        filler3 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        actionComboBox = new javax.swing.JComboBox();
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        logScrollPane = new javax.swing.JScrollPane();
        logTextArea = new javax.swing.JTextArea();
        actionPanelContainer = new javax.swing.JPanel();
        JPanel centerPanel = new JPanel();

        progressBar.setVisible(false);
        
        setLayout(new java.awt.BorderLayout());

        statusPanel.setPreferredSize(new java.awt.Dimension(400, 20));
        statusPanel.setLayout(new java.awt.GridLayout(1, 2));
        statusPanel.add(statusLabel);
        statusPanel.add(progressBar);

        add(statusPanel, java.awt.BorderLayout.SOUTH);

        actionSelectionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Action"));
        actionSelectionPanel.setPreferredSize(new java.awt.Dimension(400, 50));
        actionSelectionPanel.setLayout(new javax.swing.BoxLayout(actionSelectionPanel, javax.swing.BoxLayout.X_AXIS));
        actionSelectionPanel.add(filler3);

        actionComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Generate Keypair", "Separate Public Key", "Sign File", "Generate Certificate Request", "Generate X.509 Certificate" }));
        actionSelectionPanel.add(actionComboBox);
        actionComboBox.addItemListener(new ItemListener()
        {
            @Override
            public void itemStateChanged(ItemEvent e)
            {
                actionCommonBoxStateChanged(e);                
            }
        });
        actionSelectionPanel.add(filler2);

        add(actionSelectionPanel, java.awt.BorderLayout.NORTH);

        
        centerPanel.setLayout(new GridBagLayout());
        
        actionPanelContainer.setPreferredSize(new java.awt.Dimension(400, 300));
        actionCommonBoxStateChanged(null);
        centerPanel.add(actionPanelContainer, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
        
        logScrollPane.setBorder(javax.swing.BorderFactory.createTitledBorder("Log"));
        logScrollPane.setPreferredSize(new java.awt.Dimension(400, 300));

        logTextArea.setColumns(20);
        logTextArea.setFont(new java.awt.Font("Courier New", 0, 12));
        logTextArea.setRows(5);
        logTextArea.setEditable(false);
        logScrollPane.setViewportView(logTextArea);
        centerPanel.add(logScrollPane, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
                GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        add(centerPanel, java.awt.BorderLayout.CENTER);
    }

    private void actionCommonBoxStateChanged(ItemEvent e)
    {
        String action = (String) actionComboBox.getSelectedItem();
        actionPanelContainer.removeAll();
        actionPanelContainer.setBorder(BorderFactory.createTitledBorder(action));
        actionPanelContainer.add(actionsMap.get(action));
        actionPanelContainer.revalidate();
        actionPanelContainer.repaint();
    }

    @Override
    public void processingStarted(ProcessingStartedEvent event)
    {
        logTextArea.append(delimiter);
        progressBar.setVisible(true);
        progressBar.setIndeterminate(true);
    }
    
    @Override
    public void processingFinished(ProcessingFinishedEvent event)
    {
        logTextArea.append(delimiter);
        progressBar.setVisible(false);
        progressBar.setIndeterminate(false);
    }
    
}
